clear all
set more off
set matsize 1500

local date	"`c(current_date)'"
local user  "`c(username)'"

use "analysis.dta", clear


*Table 2*
*Pearson's chi-squared: Hypothesis that the rows and columns in a two-way table are independent
tab act exp_con, col chi2

*****************
***MAIN MODELS***
*****************

***All outcomes***

*M1*
svy: mlogit act i.exp_con, rr
est sto M1

*M2*
svy: mlogit act i.exp_con enumerator, rr
est sto M2

esttab M1 M2, keep(2.exp_con 3.exp_con 4.exp_con) se label eform wide
esttab M1 M2 using "mlogit_act.tex", keep(2.exp_con 3.exp_con 4.exp_con) b(3) se label eform wide scalars(F) star (+ 0.10 * 0.05 ** 0.01 *** 0.001) replace


***Aggregate outcome variable***

use "analysis.dta", clear

svy: mlogit act_dum i.exp_con
est sto M1_dum
svy: mlogit act_dum i.exp_con i.enumerator, baseoutcome(0)
est sto M2_dum


margins i.exp_con, l(90) post saving(exp_mlogit2.dta, replace)
use exp_mlogit2.dta, clear
lab var _predict "pr(Action)"

lab def act_dum ///
	1 "Do nothing" ///
	2 "Do something" 
lab val _predict action

*Figure 3*
format _margin %4.2f
twoway (bar _margin _predict if _predict == 1, fcolor(dkorange) lcolor(dkorange)) ///
	(bar _margin _predict if _predict == 2, fcolor(cranberry) lcolor(cranberry)) ///
	(rcap _ci_lb _ci_ub _predict, lcolor(gs12)) ///
	(scatter _margin _predict, mcolor(none) msize(zero) mlabel(_margin) ///
	mlabcolor(black) mlabposition(1) mlabgap(tiny)), by(_m1, rows(1) ///
	legend(off) note("", size(zero))) xlabel(1 "Do nothing" 2 "Do something", ///
	labsize(medlarge) angle(45)) ylabel(0(.2).8, format(%2.1f)) ///
	ymtick(##2, grid) ytitle("", size(zero)) xtitle("", size(zero)) ///
	scheme(s1color) xsize(6) ysize(2.5)
graph export "exp_mlogit_marg_dum.png", as(png) replace

*Figure 4*
twoway (bar _margin _m1 if _m1 == 1, fcolor(dknavy) lcolor(dknavy)) ///
	(bar _margin _m1 if _m1 == 2, fcolor(dkgreen) lcolor(dkgreen)) ///
	(bar _margin _m1 if _m1 == 3, fcolor(cranberry) lcolor(cranberry)) ///
	(bar _margin _m1 if _m1 == 4, fcolor(dkorange) lcolor(dkorange)) ///
	(rcap _ci_lb _ci_ub _m1, lcolor(gs12)) ///
	(scatter _margin _m1, mcolor(none) msize(zero) mlabel(_margin) ///
	mlabcolor(black) mlabposition(1) mlabgap(tiny)), ///
	xtitle("Experimental condition") xlabel(1(1)4) ///
	by(_predict, legend(on) noixtick noixlabel rows(1) note("", size(zero))) ///
	legend(region(lwidth(none)) order(1 "Stable cost of living" 2 "Fuel price increase" ///
	3 "Food price increase" 4 "Elec rate increase" 5 "90% CI") rows(1)) ///
	ylabel(0(.2).8, format(%2.1f)) ymtick(##2, grid) ytitle("Probability") ///
	scheme(s1color) xsize(6) ysize(2.5)
graph export "exp_mlogit_marg_by_action_dum.png", as(png) replace

*******************
***Risk exposure***
*******************

***No hunger***

use "analysis.dta", clear

keep if hunger == 0

mlogit act_dum i.exp_con, b(0) rr
est sto M1_dum_nohunger
mlogit act_dum i.exp_con i.enumerator, b(0) rr
est sto M2_dum_nohunger


margins i.exp_con, l(90) post saving(exp_mlogit4.dta, replace)
use exp_mlogit4.dta, clear
lab var _predict "pr(Action)"

lab def action ///
	1 "Do nothing" ///
	2 "Do something" 
lab val _predict action


*Figure 5*
format _margin %4.2f
twoway (bar _margin _predict if _predict == 1, fcolor(dkorange) lcolor(dkorange)) ///
	(bar _margin _predict if _predict == 2, fcolor(cranberry) lcolor(cranberry)) ///
	(rcap _ci_lb _ci_ub _predict, lcolor(gs12)) ///
	(scatter _margin _predict, mcolor(none) msize(zero) mlabel(_margin) ///
	mlabcolor(black) mlabposition(1) mlabgap(tiny)), by(_m1, rows(1) ///
	legend(off) note("", size(zero))) xlabel(1 "Do nothing" 2 "Do something", ///
	labsize(medlarge) angle(45)) ylabel(0(.2).8, format(%2.1f)) ///
	ymtick(##2, grid) ytitle("", size(zero)) xtitle("", size(zero)) ///
	scheme(s1color) xsize(6) ysize(2.5)
graph export "exp_mlogit_marg_dum_NO_hunger.png", as(png) width(2000) replace

use "analysis.dta", clear

***Hunger***

keep if hunger == 1

*Aggregate outcome* 
mlogit act_dum i.exp_con, b(0) rr
est sto M1_dum_hunger
mlogit act_dum i.exp_con i.enumerator, b(0) rr
est sto M2_dum_hunger


margins i.exp_con, l(90) post saving(exp_mlogit3.dta, replace)
use exp_mlogit3.dta, clear
lab var _predict "pr(Action)"

lab def act_dum_hunger ///
	1 "Do nothing" ///
	2 "Do something" 
lab val _predict action

*Figure 6*
format _margin %4.2f
twoway (bar _margin _predict if _predict == 1, fcolor(dkorange) lcolor(dkorange)) ///
	(bar _margin _predict if _predict == 2, fcolor(cranberry) lcolor(cranberry)) ///
	(rcap _ci_lb _ci_ub _predict, lcolor(gs12)) ///
	(scatter _margin _predict, mcolor(none) msize(zero) mlabel(_margin) ///
	mlabcolor(black) mlabposition(1) mlabgap(tiny)), by(_m1, rows(1) ///
	legend(off) note("", size(zero))) xlabel(1 "Do nothing" 2 "Do something", ///
	labsize(medlarge) angle(45)) ylabel(0(.2).8, format(%2.1f)) ///
	ymtick(##2, grid) ytitle("", size(zero)) xtitle("", size(zero)) ///
	scheme(s1color) xsize(6) ysize(2.5)
graph export "exp_mlogit_marg_dum_hunger.png", as(png) width(2000) replace


exit

